import requests
rawTxt=open('words.txt','r')
resultTxt=open('result.txt','w',encoding='UTF-8')

#获取结果处理数据
def getResult(data):
    payload = {'re': 'true', 'content': data}                         #传入参数
    r = requests.post("https://pt.hongliang.fun/api/v1/pt",params=payload).json()['data']

    # 遍历每个单词
    for item in r:
        if item[1]:resultTxt.write(item[0] + '\n[' +item[1] + ']\n')  #--01有音标时
        else:resultTxt.write(item[0] + '\n')                          #--02无音标时

    # 遍历每个词意
        meanings=item[5]
        if meanings:                                                  #--01有词意时
            for i in range(len(meanings)):
                attribute=meanings[i][0]
                chinesetxt=meanings[i][1].split('；')
                resultTxt.write(attribute+ ' '+chinesetxt[0]+'；')
        else:resultTxt.write(item[6])                                 #--02无词意时
        resultTxt.write('\n\n')

#调整格式
wordslist=[]
for line in rawTxt:
    line=line.rstrip()
    if line != '' :                 #忽略空行
        wordslist.append(line)
data=' '.join(wordslist)            #转为字符串
getResult(data)



